var flow_db = require('../db/flowdb')
var pu = require('../tool/public')
var jurUpdate = require('../tool/jurUpdate')
var processCircle = require('../process/processCircle')

/**
 * @Description:  flow
 * @author longfa liang
 * @date 2021/9/14
 */
class flow {


    /*新增流程*/
    async add_flow (req, res, next) {
        var name = req.body.name //流程名称
        var team_id = req.headers.team //团队id
        var userid = req.headers.uid //新增用户id
        var username = req.body.username //新增人名字
        var icon = req.body.icon //图标
        var jurlist = req.body.jurlist //权限列表
        var flow = req.body.flow//节点
        var temp_id = req.body.temp_id ? req.body.temp_id : null//模板id
        var temp = req.body.temp//模板
        var temp_status = req.body.temp_status//模板
        var lin = req.body.line//线条
        var line_condition = req.body.line_condition //线条条件
        var next_process_status = req.body.next_process_status//是否自动触发下流程
        var next_process_name = req.body.next_process_name//自动触发下流程name
        var next_process_id = req.body.next_process_id ? req.body.next_process_id : null//自动触发下流程id
        var next_temp_condition = req.body.next_temp_condition// 条件
        var up_process_status = req.body.up_process_status //是否自动发起
        var up_process_type = req.body.up_process_type // 0根据流程 1根据表单
        var up_process_process_id = req.body.up_process_process_id  ? req.body.up_process_process_id : null// 流程id
        var up_process_process_name = req.body.up_process_process_name // 流程name
        var up_temp_id = req.body.up_temp_id ? req.body.up_temp_id : null // 上模板id
        var up_temp_name = req.body.up_temp_name // 上模板名称         var up_temp_field = req.body.up_temp_field // 上模板字段
        var up_temp_condition = req.body.up_temp_condition //条件 [{field:"1234",condition:"",value:""}]上模板字段 大于等于： gte 大于：gt  小于等于：lte 小于： lt
        var up_temp_condition_status = req.body.up_temp_condition_status //是否需要条件
        var set_data = req.body.set_data //高级设置
        var module = req.body.module // 所属模块
        let isnull = pu.isnull([name, username, icon, jurlist, flow, lin, next_process_status,module])
        // let setisnull = pu.isnull([])
        if (isnull) {
            pu.fail('参数不正确', res)
            return
        }
        if (!flow.length || !lin.length) {
            pu.fail('参数不正确', res)
            return
        }
        let info = await flow_db.findOne('flow_temp', { team_id: flow_db.ObjectID(team_id), name: name })

        if (info) {
            pu.fail('名称已经存在', res)
            return
        }
        // 流程流转
        if( next_process_status === 1 || next_process_name || next_process_id){
            let data = await flow_db.findOne('flow_temp', { temp_id: flow_db.ObjectID(next_process_id), name: next_process_name })
            if(!data){
                pu.fail('该流程不存在', res)
                return
            }
            processCircle.circle.circulation(data,next_temp_condition)
        }
        // process_approve.approve_succesc(pr_mg.ObjectID(process_id), process.template, [{ userid: userid, username: username }], status, describe, nodeid)
        var inserinfo = {
            name: name,
            team_id: flow_db.ObjectID(team_id),
            userid: flow_db.ObjectID(userid),
            username: username,
            icon: icon,
            jurlist: jurlist,
            flow: flow,
            lin: lin,
            line_condition:line_condition,
            version: 1,
            lastUpdateTime: +new Date(),
            temp_status: temp_status,
            next_process_status: next_process_status,
            next_process_name: next_process_name,
            next_process_id: next_process_id ? flow_db.ObjectID(next_process_id) : '',
            next_temp_condition: next_temp_condition,
            up_process_status: up_process_status,
            up_process_type: up_process_type,
            up_process_process_id: up_process_process_id ? flow_db.ObjectID(up_process_process_id) : '',
            up_process_process_name: up_process_process_name,
            up_temp_id: up_temp_id ? flow_db.ObjectID(up_temp_id) : '',
            up_temp_name: up_temp_name,
            up_temp_condition: up_temp_condition,
            up_temp_condition_status: up_temp_condition_status,
            temp: temp,
            temp_id: flow_db.ObjectID(temp_id),
            set_data:set_data, // 高级设置
            module:module,
            status: 0, //是否启用  0启用
            is_delete: 0, //是否删除  0未删除 1删除
            new_tatus:0 //处理状态  0处理中  1处理完成 2驳回 3撤回 4 作废
        }
        let inset = await flow_db.insertOne('flow_temp', inserinfo)
        var newteam = {
            name: name,
            server: 'flow',
            status: 0,
            url:pu.uuid(),
            _id: inserinfo.temp_id,
            type: 'flow',
            adopt: ['*'],
            field: ['*'],
            jurlist: []
        }
        // 把流程单添加在左边菜单栏
        jurUpdate.jurupdate(team_id,newteam,module,0)
        inserinfo._id = inset.insertedId
        pu.success('成功', inserinfo, res)
    }
    // 查找流程
    async find_flow (req, res, next) {
        var team_id = req.headers.team //团队id
        var status = req.body.status //状态
        var is_delete = req.body.is_delete //删除状态
        var temp_id = req.body.temp_id
        var userid = req.body.userid
        var where = {
            team_id: flow_db.ObjectID(team_id)
        }
        if (status || status == 0) {
            where.status = status
        }

        if (is_delete || is_delete == 0) {
            where.is_delete = is_delete
        }
        if (temp_id) {
            where.temp_id = flow_db.ObjectID(temp_id)
        }
        if (userid) {
            where.userid = flow_db.ObjectID(userid)
        }
        console.log(where,'条件');
        let list = await flow_db.find('flow_temp', where)
        pu.success('成功', list, res)

    }
    //  修改流程
    async update_flow (req, res, next) {
        let _id = req.body._id
        var updateinfo = req.body.updateinfo
        var keys = ['name', 'temp_status', 'icon', 'jurlist', 'flow', 'lin', 'next_process_status', 'next_process_name', 'next_process_id', 'up_process_status', 'up_process_type', 'up_process_process_id', 'up_process_process_name', 'up_temp_id', 'up_temp_name', 'up_temp_condition', 'up_temp_condition_status', 'temp', 'temp_id', 'status', 'is_delete']
        var update = {}
        for (var i = 0; i < keys.length; i++) {
            if (updateinfo[keys[i]] !== undefined) {
                update[keys[i]] = updateinfo[keys[i]]
            }
        }
        let flowinfo = await flow_db.findOne('flow_temp', { _id: flow_db.ObjectID(_id) })
        update.lastUpdateTime = +new Date()
        update.version = flowinfo.version + 1
        await flow_db.updateOne('flow_temp', { _id: flow_db.ObjectID(_id) }, { $set: update })
        pu.success('OK', update, res)
    }
}


module.exports = new flow()
